package com.gitee.cashzhang27.test.boot.other.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;

/**
 * @author cash Zhang
 * @since 2019/01/28 15:49
 */
public class PdfBoxUtils {

  private static final String PNG_SUFFIX = ".png";

  public static String[] pdf2Image(String pdfDestFilePath, String destDirPath,
      String zfhth) throws IOException {
    File pdfFile = new File(pdfDestFilePath);
    PDDocument doc = PDDocument.load(pdfFile);
    PDFRenderer renderer = new PDFRenderer(doc);
    int pageCount = doc.getNumberOfPages();
    String[] imageArr = new String[pageCount];
    for (int i = 0; i < pageCount; i++) {
      BufferedImage image = renderer.renderImageWithDPI(i, 296);
//          BufferedImage image = renderer.renderImage(i, 2.5f);
      File file = new File(destDirPath + File.separator + zfhth + "_" + i + PNG_SUFFIX);
      ImageIO.write(image, "PNG", file);
      imageArr[i] = file.getPath();
    }
    return imageArr;
  }
}
